package data_daos

import (
	"errors"
	"log"
	"rabbit_love/src/model"
	"rabbit_love/src/service/client/forms/request/data_forms"
	"rabbit_love/src/service/client/utils"
	"rabbit_love/src/utils/oss"
)

func GetDataListDao(loverId int, form *data_forms.GetDataRequest) ([]map[string]interface{}, error) {
	var (
		page  int
		limit int
	)

	if form.Page == 0 {
		page = 1
	} else {
		page = form.Page
	}

	if form.Limit == 0 {
		limit = 20
	} else {
		limit = form.Limit
	}

	var (
		offset   = (page - 1) * limit
		data     []map[string]interface{}
		clientDB = utils.ClientDB
		err      error
	)

	if form.Type == model.Sign {
		if err = clientDB.Model(&model.DailyTable{}).
			Select("`id` ,`data`, `title`, `type`, `userId`, `picUrl`, `read`").
			Where("`loverRelationId` = ? and `type` = ?", loverId, model.Sign).
			Update("read", 1).
			Order("createdAt desc").
			Limit(limit).
			Offset(offset).
			Find(&data).Error; err != nil {

			log.Println("GetDataListDao Err: ", err.Error())
			return nil, errors.New("查询失败")
		}
	} else if form.Type == model.Daily {
		if err = clientDB.Model(&model.DailyTable{}).
			Select("`id`, `data`, `title`, `type`, `userId`, `picUrl`, `read`, `createdAt`").
			Where("`loverRelationId` = ? and `type` = ?", loverId, model.Daily).
			Update("read", 1).
			Limit(limit).
			Order("createdAt desc").
			Offset(offset).
			Find(&data).Error; err != nil {

			log.Println("GetDataListDao Err: ", err.Error())
			return nil, errors.New("查询失败")
		}
	} else {

		err = errors.New("请求参数错误")
	}

	return data, err
}

func AddDataListDao(loverId, userId int, form *data_forms.AddDataRequest) error {
	var (
		clientDB = utils.ClientDB
		err      error
	)

	data := &model.DailyTable{
		LoverRelationTableId: int64(loverId),
		UserID:               int64(userId),
		Title:                form.Title,
		Data:                 form.Data,
		Type:                 form.Type,
	}
	if form.Pic != nil {
		filePath, err := oss.SaveFileToOSS(form.Pic)
		if err != nil {
			log.Println("AddDataListDao Err: ", err.Error())
			return errors.New("文件上传失败")
		}

		data.PicUrl = filePath
	}

	if err = clientDB.Create(&data).Error; err != nil {
		log.Println("AddDataListDao Err: ", err.Error())
		return errors.New("发表失败")
	}
	return nil
}

func UpDataListDao(loverId, userId int, form *data_forms.UpdDataRequest) error {
	var (
		clientDB = utils.ClientDB
		data     *model.DailyTable
		err      error
	)

	if clientDB.Where("loverRelationId = ? and userId = ? and id = ?", loverId, userId, form.DataId).First(&data); data == nil {
		return errors.New("没有找到对应的数据")
	}

	if form.Pic != nil {
		if data.PicUrl != "" {
			oss.DelFile(data.PicUrl)
		}

		var filePath string
		filePath, err = oss.SaveFileToOSS(form.Pic)
		if err != nil {
			log.Println("AddDataListDao Err: ", err.Error())
			return errors.New("文件上传失败")
		}

		data.PicUrl = filePath
	}

	if form.Title != "" {
		data.Title = form.Title
	}

	if form.Data != "" {
		data.Data = form.Data
	}

	if clientDB.Save(&data).Error != nil {
		return errors.New("文件上传失败")
	}

	return nil
}
